/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.util;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import rpg.gfx.Bitmap;

/**
 * rothens.tarhely.biz
 *
 * @author Rothens
 */
public class ImageUtil {

	/**
	 * Creates a BufferedImage from the given resource path.
	 *
	 * @param path
	 * @return a BufferedImage, or null if not found
	 */
	public static BufferedImage createImageIcon(String path) {
		try {
			return ImageIO.read(ImageUtil.class.getResource(path));
		} catch (IOException ex) {
			System.err.println("Couldn't read image from " + path);

		}
		return null;
	}

	public static Bitmap getSub(BufferedImage img, int row, int col) {
		Bitmap ret = new Bitmap(64, 64);

		img.getSubimage(col * 64, row * 64, 64, 64).getRGB(0, 0, ret.width, ret.height, ret.pixels, 0, ret.width);

		return ret;
	}

	public static Bitmap getSub(BufferedImage img, int row, int col, int w, int h) {
		Bitmap ret = new Bitmap(w, h);

		img.getSubimage(col * w, row * h, w, h).getRGB(0, 0, ret.width, ret.height, ret.pixels, 0, ret.width);

		return ret;
	}

	public static Bitmap loadBitmap(String path) {
		BufferedImage src = null;

		try {
			src = ImageIO.read(ImageUtil.class.getResource(path));
		} catch (IOException ex) {
			System.err.println("Couldn't read image from " + path);
			return null;
		}
		Bitmap b = new Bitmap(src.getWidth(), src.getHeight());
		src.getRGB(0, 0, b.width, b.height, b.pixels, 0, b.width);
		return b;
	}

	public static Bitmap loadBitmapScaled(String path, double scale) {
		BufferedImage src = null;

		try {
			src = ImageIO.read(ImageUtil.class.getResource(path));
		} catch (IOException ex) {
			System.err.println("Couldn't read image from " + path);
			return null;
		}
		BufferedImage sc = toBufferedImage(src.getScaledInstance((int) (src.getWidth() * scale), (int) (src.getHeight() * scale), Image.SCALE_DEFAULT));
		Bitmap b = new Bitmap(sc.getWidth(), sc.getHeight());
		sc.getRGB(0, 0, b.width, b.height, b.pixels, 0, b.width);
		return b;
	}

	public static BufferedImage toBufferedImage(Image image) {
		if (image instanceof BufferedImage) {
			return (BufferedImage) image;
		}

		// This code ensures that all the pixels in the image are loaded
		image = new ImageIcon(image).getImage();

		// Determine if the image has transparent pixels; for this method's
		// implementation, see Determining If an Image Has Transparent Pixels
		boolean hasAlpha = false;

		// Create a buffered image with a format that's compatible with the screen
		BufferedImage bimage = null;
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		try {
			// Determine the type of transparency of the new buffered image
			int transparency = Transparency.OPAQUE;
			if (hasAlpha) {
				transparency = Transparency.BITMASK;
			}

			// Create the buffered image
			GraphicsDevice gs = ge.getDefaultScreenDevice();
			GraphicsConfiguration gc = gs.getDefaultConfiguration();
			bimage = gc.createCompatibleImage(
				image.getWidth(null), image.getHeight(null), transparency);
		} catch (HeadlessException e) {
			// The system does not have a screen
		}

		if (bimage == null) {
			// Create a buffered image using the default color model
			int type = BufferedImage.TYPE_INT_RGB;
			if (hasAlpha) {
				type = BufferedImage.TYPE_INT_ARGB;
			}
			bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
		}

		// Copy image to buffered image
		Graphics g = bimage.createGraphics();

		// Paint the image onto the buffered image
		g.drawImage(image, 0, 0, null);
		g.dispose();

		return bimage;
	}
}
